package com.matt.foundation.sqlOperator;

import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.utils.Validations;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/10/16
 * *******************************
 */
public class ContractOperator {

//    public static final String remoteUrl = "jdbc:mysql://rm-bp1xxa9jd5ajt2km8o.mysql.rds.aliyuncs.com:3306/vega";
//
//    public static final String remoteName = "matt";
//
//    public static final String remotePWd = "HgY5CcO4";

        public static final String remoteUrl = "jdbc:mysql://localhost:3306/vega";

    public static final String remoteName = "root";

    public static final String remotePWd = "123456";

//pro
//    public static final String remoteUrl = "jdbc:mysql://rm-bp151a4j302p495nso.mysql.rds.aliyuncs.com:3306/vega_prd";
////
//    public static final String remoteName = "matt";
////
//    public static final String remotePWd = "HgY5CcO4";

    public static void main(String[] args) {
        processCode();
    }


    public static void processCode() {
        int num = 0;


        /**
         * 需要根据月份不断的查
         */
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection remoteConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);
            PreparedStatement remotePreparedStatement = null;
            ResultSet remoteResultSet = null ;
            String query = "select c.id, c.cid, c.name, c.duration from children_extend_info e left join children cc on e.childrenId = cc.id LEFT JOIN contract c on cc.id = c.cid where e.nurseryStatus = -1 and e.earlyLeftCourseNum = -1";
            remotePreparedStatement = remoteConn.prepareStatement(query);

            remoteResultSet = remotePreparedStatement.executeQuery();
            Connection localConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);
            PreparedStatement localPreparedStatement = null;
            ResultSet localResultSet = null;
            while (remoteResultSet.next()){
                String id = remoteResultSet.getString(1); //cid
                String childrenId = remoteResultSet.getString(2); //cid
                String name = remoteResultSet.getString(3); //aName
                String duration = remoteResultSet.getString(4); //duration


                if ("早教".equals(name)){
//                    String update = "update children_extend_info  set earlyTotalCourseNum=?, earlyLeftCourseNum=? where childrenId=? ";
//                    localPreparedStatement = localConn.prepareStatement(update);
//
//                    localPreparedStatement.setInt(1, Integer.valueOf(duration));
//                    localPreparedStatement.setInt(2, Integer.valueOf(duration));
//                    localPreparedStatement.setString(3, childrenId);
//                    localPreparedStatement.execute();
//
//                    String conUpdate = "update contract  set name=? where cid=? ";
//                    localPreparedStatement = localConn.prepareStatement(conUpdate);
//
//                    localPreparedStatement.setString(1, "SAT早教");
//                    localPreparedStatement.setString(2, childrenId);
//                    localPreparedStatement.execute();


                }else if ("全托".equals(name) || "半托".equals(name) || "临托".equals(name)){
                    String update = "update children_extend_info  set nurseryStatus=? where childrenId=? ";
                    localPreparedStatement = localConn.prepareStatement(update);
                    localPreparedStatement.setInt(1, 1);
                    localPreparedStatement.setString(2, childrenId);
                    localPreparedStatement.execute();

                    if (Validations.validateInt(duration)){
                        String contractUpdate = "update contract  set duration=? where cid=? ";
                        localPreparedStatement = localConn.prepareStatement(contractUpdate);
                        System.out.println("id :" +id);
                        System.out.println("name :" +name);
                        System.out.println("childrenId :" + childrenId);
                        System.out.println("duration :" + duration);
                        localPreparedStatement.setString(1, duration + "个月");
                        localPreparedStatement.setString(2, childrenId);
                        localPreparedStatement.execute();
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }


//    该城市的行政区划代码(两位数字)+该城市的园区顺序号(三位数字)+年份(4 位数字)+月份(两位数字)+入园学生顺序号(三位数字)+性别(1 位数字)。
//    北京11  上海31  成都51
//            园区及学生顺序按照创建时间排序
//    性别: 男1 女2
}
